package com.open.jack.jk_mpchart

import android.graphics.Color
import android.util.SparseArray
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.formatter.IAxisValueFormatter
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet

class JkLineChartUtil {
    private val colors = SparseArray<Int>()
    private val xUnitStringMap = HashMap<Int, String>()
    private val typeList = mutableListOf<Int>()
    private val source: SparseArray<List<Float>> = SparseArray()

    private lateinit var chartTrend:JkLineChart

    open fun initCharView(lineChart: JkLineChart) {
        this.chartTrend = lineChart
        val emptyData = LineData()
        lineChart.setData(emptyData)
        lineChart.animateXY(2000, 2000)

        val xl: XAxis = lineChart.getXAxis().apply {
        }
        //xl.axisMinimum = 0f
        //xl.axisMaximum = 100f
        xl.textColor = Color.parseColor("#5A6BC8")
        xl.gridColor = Color.parseColor("#5A6BC8")
        xl.setGranularity(1f)
        lineChart.xAxis.valueFormatter = object : IAxisValueFormatter {
            override fun getFormattedValue(value: Float, axis: AxisBase?): String {
                return xUnitStringMap.get(value.toInt())?:""
            }
        }

        val leftAxis: YAxis = lineChart.getAxisLeft().apply {
            setGranularity(1f)
//            setAxisMinimum(0f)//强制 Y 轴从 0 开始。
            spaceTop = 15f
        }
        xl.axisLineColor = Color.parseColor("#5A6BC8")
        leftAxis.axisLineColor = Color.parseColor("#5A6BC8")
        leftAxis.textColor = Color.parseColor("#5A6BC8")
        leftAxis.gridColor = Color.parseColor("#5A6BC8")

        // don't forget to refresh the drawing

        //#57AEFF
        // setData(10, 100f)
        //request()
    }

    fun clear(){
        colors.clear()
        xUnitStringMap.clear()
        source.clear()
        typeList.clear()
    }
    fun setXAxisLabel(xIndex:Int,label:String){
        xUnitStringMap.set(xIndex,label)
    }

    fun setLeftAxisTextColor(color:String){
        // 获取左侧 Y 轴对象
        val leftAxis: YAxis = chartTrend.getAxisLeft()
        // 设置 Y 轴标签文字颜色
        leftAxis.textColor = Color.parseColor(color)
    }

    fun setXAxisTextColor(color:String){
        // 获取 X 轴对象
        val xAxis: XAxis = chartTrend.getXAxis()
       // 设置 X 轴标签文字颜色
        xAxis.textColor =  Color.parseColor(color)
    }


    fun setLineColor(type:Int,color:String) {
        colors.put(type,Color.parseColor(color))
    }

    fun addLineData(type:Int,linesData:List<Float>){
        typeList.add(type)
        source.put(type,linesData)
    }

    fun updateView(){
        val sets = getLineSets(typeList, source)
        val lineData = LineData(sets)
        chartTrend.apply {
            data = lineData
            invalidate()
        }
    }


    private  fun getLineSets(
        types: List<Int?>,
        source: SparseArray<List<Float>> //typelist
    ): ArrayList<ILineDataSet> {
        val sets: ArrayList<ILineDataSet> = ArrayList<ILineDataSet>()
        if (types.isNotEmpty()) {
            //val legendList = ArrayList<LegendBean>()
            types.forEach { it ->
                val type = it?.toInt()
                if (type == null) return@forEach
                val yVals = source.get(type)
                if (yVals != null) {
                    val entries = ArrayList<Entry>()
                    yVals.forEachIndexed { index, yYal ->
                        entries.add(Entry(index.toFloat(), yYal))
                    }
                    //val lineColor = getRandomColor()
                    //legendList.add(LegendBean(lineColor, getc(type)))
                    sets.add(
                        JkLineChart.getLineDataSet(
                            entries,
                            colors.get(type),
                            JkLineChart.highLightColor,
                            "DataSet $type"
                        )
                    )
                }
            }
            //mLegendAdapter?.clearAll()
            //mLegendAdapter?.addAll(legendList)
        }
        return sets
    }
}